<template>
  <div class="printForm">
    <div class="area-header">
      <span class="area-header-title">{{ $t('申请单') }}</span>
    </div>
    <table class="printTable">
      <tr>
        <td class="tit">{{ $t('申请标题') }}</td>
        <td colspan="3">{{ form.actTitle }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('项目名称') }}</td>
        <td>
          {{ form.dealBase.dealName }}
        </td>

        <td class="tit">{{ $t('总投资额') }}{{ $t('金额单位') }}</td>
        <td>
          {{ moneyFormat(form.totalInvestment, 'w') }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('项目负责人') }}</td>
        <td>
          {{ form.dealUser.nickName }}
        </td>

        <td class="tit">{{ $t('所属部门') }}</td>
        <td>
          {{ form.dealDept.deptName }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('投资时间') }}</td>
        <td>
          {{ parseTime(form.investmentTime) }}
        </td>

        <td class="tit">{{ $t('最新持股比例(%)') }}</td>
        <td>
          {{ form.latestShareholdingRatio }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('在投成本') }}{{ $t('金额单位') }}</td>
        <td>
          {{ moneyFormat(form.reinvestmentCost, 'w') }}
        </td>

        <td class="tit">{{ $t('已退出总额') }}{{ $t('金额单位') }}</td>
        <td>
          {{ moneyFormat(form.exitTotal, 'w') }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('拟退出时间') }}</td>
        <td>
          {{ parseTime(form.draftExitTime) }}
        </td>
        <td class="tit">
          {{ $t('拟退出持股比例(%)') }}
        </td>

        <td>
          {{ form.draftExitShareholdingRatio }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('拟退出金额') }}{{ $t('金额单位') }}</td>
        <td>
          {{ moneyFormat(form.draftExitPrice, 'w') }}
        </td>
        <td class="tit">{{ $t('优先退出方式') }}</td>
        <td>
          <dict-select
            v-model="form.priorWay"
            dictionary="exit_plan_item"
            :placeholder="$t('优先退出方式')"
          />
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('备选退出方式') }}</td>
        <td>
          <dict-select
            v-model="form.spareWay"
            dictionary="exit_plan_item"
            :placeholder="$t('备选退出方式')"
          />
        </td>
        <td class="tit">{{ $t('保底退出方式') }}</td>
        <td>
          <dict-select
            v-model="form.lowestWay"
            dictionary="exit_plan_item"
            :placeholder="$t('保底退出方式')"
          />
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('备注') }}</td>
        <td colspan="3">
          {{ form.remark }}
        </td>
      </tr>
    </table>

    <FileView :list.sync="form.fileList" data-type="deal_exit" :btn-show="!otherBtnHide" />

  </div>
</template>

<script>
    import { saveDealExit, getDealExit, getExitBaseInfo } from '@/api/sass/kaihui/exit/kaiHuiDealExit'
    import { getDealName } from '@/api/base/deal/dealBase'

    export default {
        data() {
            return {
              // 退出方式字典
              exitModeOptions: [], // 表单参数
              form: {
                dealBase: {
                  dealName: ''
                },
                dealUser: {
                  nickName: ''
                },
                dealDept: {
                  deptName: ''
                },
                // 附件列表
                fileList: []
              },
              // 根据退出方式选择是否展示受让方
              isShowTransferee: false,
              // 是否隐藏保存按钮
              btnHide: false,
              // 是否隐藏其它按钮
              otherBtnHide: false,
              // 表单校验
              rules: {
                draftExitTime: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], draftExitShareholdingRatio: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], draftExitPrice: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], exitMode: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], transferee: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], actTitle: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ] },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true
            }
        },
        created() {
            this.getDicts('exit_mode').then(response => {
                this.exitModeOptions = response.data
            })
            this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined && data.id !== '' && data.id !== null) {
                    getDealExit(data.id).then(response => {
                        this.form = response.data
                        this.exitModelChange(this.form.exitMode)
                        this.loading = false
                    })
                } else {
                    this.reset()
                    // 默认带出流程中必须要传的附件
                    if (data !== null) {
                      this.form.fileList = data.fileList
                    }
                    this.exitModelChange('')
                    getExitBaseInfo(this.$route.query.id).then(response => {
                      if (response.data != null) {
                        this.form = response.data
                        this.form.id = undefined
                      }
                      this.loading = false
                    })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealBase: {
                  id: this.$route.query.id,
                  dealName: ''
                },
                totalInvestment: undefined,
                userId: undefined,
                dealUser: {
                  id: undefined,
                  nickName: ''
                },
                deptId: undefined,
                dealDept: {
                  id: undefined,
                  deptName: ''
                },
                investmentTime: undefined,
                latestShareholdingRatio: undefined,
                reinvestmentCost: undefined,
                exitTotal: undefined,
                draftExitTime: undefined,
                draftExitShareholdingRatio: undefined,
                draftExitPrice: undefined,
                exitMode: undefined,
                exitSharesNumber: undefined,
                expectPerSharePrice: undefined,
                transferee: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm(type) {
                // 关闭form页面遮罩层
              this.loading = false
              if (type !== 'actSave') {
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
              }
            },
            /** 提交按钮 */
            submitForm: function(type) {
              return new Promise((resolve, reject) => {
                this.$refs['form'].validate(valid => {
                  // 验证数据
                  if (valid) {
                    // 弹出遮罩层
                    this.loading = true
                    // 保存方式，如果值为actSave则是从流程表单中进行的保存，默认将delFlag 设置为2,审批通过后在更新为0
                    this.form.saveType = type
                    // 保存数据
                    saveDealExit(this.form).then(response => {
                      if (response.code === 200) {
                        // 点击的按钮是保存并启动流程
                        if (type === 'process') {
                          // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                          this.form.id = response.data.id
                          // 启动流程
                          this.startProcess(response.data.id)
                          // 点击的仅保存
                        } else {
                          this.msgSuccess(this.$t('保存成功'))
                          // 关闭form页面
                          this.closeForm(type)
                        }
                        resolve(response.data.id)
                      } else {
                        this.msgError(response.msg)
                        this.loading = false
                        reject(response)
                      }
                    }).catch(() => {
                      this.loading = false
                    })
                  } else {
                    reject()
                  }
                })
              })
            },
            // 启动流程
           startProcess: async function(dataId) {
               var dealName = await getDealName(this.$route.query.id)
               this.loading = false
               // 启动流程时候传入的参数
               const processData = {}
               // 待办标题
               processData.title = '「' + dealName + '」' + ':' + this.form.actTitle
               // 业务表名
               processData.dataTable = 'deal_exit'
               // 业务ID
               processData.dataId = dataId
               // 业务表单地址(待办页面需要打开的页面)
             processData.formPath = '@/views/saas/kaihui/exit/kaiHuiDealExitForm'
             processData.viewPath = '@/views/saas/kaihui/exit/kaiHuiDealExitView'
               // 菜单标识(根据菜单标识判断的走哪个流程)
               processData.menuKey = 'deal_exit'
               this.startProcessByMenu(processData, () => {
                   this.form.actStatus = 'D'
                   this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
                   this.closeForm()
               })
           },
          exitModelChange(val) {
              if (val === 'merger' || val === 'transfer_shares') {
                this.isShowTransferee = true
              } else {
                this.isShowTransferee = false
              }
          },
          // 退出方式字典翻译
          exitModeFormat(row) {
            return this.selectDictLabel(this.exitModeOptions, row)
          },
          // 预计每股均价 = 拟退出金额/退出股数
          expectPerSharePriceCount() {
              if (this.form.exitSharesNumber !== 0) {
                this.form.expectPerSharePrice = this.form.draftExitPrice / this.form.exitSharesNumber
              }
          }
        }

    }
</script>
